﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DAL;
using Cocbook_DTO;

namespace Sale_and_manage
{
    public partial class frmBookFilter : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmBookFilter()
        {
            InitializeComponent();
            LoadProjectName();
            LoadAuthor();
            LoadGenre();
            LoadIssuer();
            LoadPublisher();
            SetColumnWidth();
        }

        public static int type = 0;
        public List<Project> lpr;
        public List<string> lishPublisher;
        public List<string> listIssuer;
        public List<Author> listAuthor;
        public List<string> listGenre;
        public List<Book_FilterView> lbfv;
        public List<Book_Filter> listBo_Fi;
        public Int32 rowClicked = -1;
        public delegate void chooseBookDlg(Int64 ISBN);
        public event chooseBookDlg chooseBookEvent;
        public bool Sold = true;
        public bool Price = true;

        public void SetColumnWidth()
        {
            dgvFilter.Columns[0].Width = 95;
            dgvFilter.Columns[1].Width = 190;
            dgvFilter.Columns[2].Width = 130;
            dgvFilter.Columns[3].Width = 130;
            dgvFilter.Columns[4].Width = 45;
            dgvFilter.Columns[5].Width = 140;
            dgvFilter.Columns[6].Width = 140;
            dgvFilter.Columns[7].Width = 35;
        }

        private void btnTopBestSale_Click(object sender, EventArgs e)
        {
            BookDAL bDAL = new BookDAL();
            if (cboProjectName1.SelectedIndex == 0)
            {
                Sold = !Sold;
                lbfv = bDAL.GetAllBookByFilter(0,0,"","","","","",0,0,"Sold", Sold);
            }
            else
            {
                Sold = !Sold;
                lbfv = bDAL.GetAllBookByFilter(lpr[cboProjectName1.SelectedIndex-1].ProjectID,0,"","","","","",0,0, "Sold", Sold);
            }
            ConvertAndLoad();            
        }

        private void btnTopGoodPrice_Click(object sender, EventArgs e)
        {
            Project project = new Project(0);
            if (cboProjectName1.SelectedIndex != 0)
            {
                project = lpr[cboProjectName1.SelectedIndex - 1];
            }
            Int64 isbn = 0;
            if (txtISBN.Text != "")
            {
                isbn = Convert.ToInt64(txtISBN.Text);
            }
            string publisher = "";
            if (cboPublisher.SelectedIndex != 0)
            {
                publisher = cboPublisher.Text;
            }
            string title = "";
            if (txtTitle.Text != "")
            {
                title = txtTitle.Text;
            }
            string author = "";
            if (cboAuthor.SelectedIndex != 0)
            {
                author = cboAuthor.Text;
            }
            string issuer = "";
            if (cboIssuer.SelectedIndex != 0)
            {
                issuer = cboIssuer.Text;
            }
            string genre = "";
            if (cboGenre.SelectedIndex != 0)
            {
                genre = cboGenre.Text;
            }
            Int32 lowPrice = 0;
            if (txtLowPrice.Text != "")
            {
                lowPrice = Convert.ToInt32(txtLowPrice.Text);
            }
            Int32 highPrice = 0;
            if (txtHighPrice.Text != "")
            {
                highPrice = Convert.ToInt32(txtHighPrice.Text);
            }
            BookDAL bDAL = new BookDAL();
            if (cboProjectName1.SelectedIndex == 0)
            {
                Price = !Price;
                lbfv = bDAL.GetAllBookByFilter(0, isbn, publisher, title, author, issuer, genre, lowPrice, highPrice, "Price", Price);
            }
            else
            {
                Price = !Price;
                lbfv = bDAL.GetAllBookByFilter(project.ProjectID, isbn, publisher, title, author, issuer, genre, lowPrice, highPrice, "Price", Price);
            }
            ConvertAndLoad();
        }

        private void txtISBN_TextChanged(object sender, EventArgs e)
        {
        }

        private void cboPublisher_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoaddgvFilterByAdvanceFilter();
        }

        private void txtBookname_TextChanged(object sender, EventArgs e)
        {
        }

        private void cboAuthor_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoaddgvFilterByAdvanceFilter();
        }


        
        public void LoaddgvFilterByAdvanceFilter()
        {
            if (superTabControl1.SelectedTabIndex != 0)
            {
                Project project = new Project(0);
                if (cboProjectName1.SelectedIndex != 0)
                {
                    project = lpr[cboProjectName1.SelectedIndex - 1];
                }
                Int64 isbn = 0;
                if (txtISBN.Text != "")
                {
                    isbn = Convert.ToInt64(txtISBN.Text);
                }
                string publisher = "";
                if (cboPublisher.SelectedIndex != 0)
                {
                    publisher = cboPublisher.Text;
                }
                string title = "";
                if (txtTitle.Text != "")
                {
                    title = txtTitle.Text;
                }
                string author = "";
                if (cboAuthor.SelectedIndex != 0)
                {
                    author = cboAuthor.Text;
                }
                string issuer = "";
                if (cboIssuer.SelectedIndex != 0)
                {
                    issuer = cboIssuer.Text;
                }
                string genre = "";
                if (cboGenre.SelectedIndex != 0)
                {
                    genre = cboGenre.Text;
                }
                Int32 lowPrice = 0;
                if (txtLowPrice.Text != "")
                {
                    lowPrice = Convert.ToInt32(txtLowPrice.Text);
                }
                Int32 highPrice = 0;
                if (txtHighPrice.Text != "")
                {
                    highPrice = Convert.ToInt32(txtHighPrice.Text);
                }
                BookDAL bDAL = new BookDAL();
                if (cboProjectName1.SelectedIndex == 0)
                {
                    lbfv = bDAL.GetAllBookByFilter(0,isbn, publisher, title, author, issuer, genre, lowPrice, highPrice);
                }
                else
                {
                    lbfv = bDAL.GetAllBookByFilter(project.ProjectID, isbn, publisher, title, author, issuer, genre, lowPrice, highPrice);
                }
                ConvertAndLoad();
            }
        }

        public void LoadProjectName()
        {
            ProjectDAL pDAL = new ProjectDAL();
            lpr = pDAL.GetAllProject();
            cboProjectName1.Items.Add("Tất cả");
            int i = 0;
            int j = 0;
            foreach (Project pr in lpr)
            {
                i = i + 1;
                cboProjectName1.Items.Add(pr.ProjectName);
                if (pr.ProjectName == "Order" && type == 1)
                {
                    j = i;
                }
            }
            if (type == 1)
            {
                cboProjectName1.SelectedIndex = j;
                cboProjectName1.Enabled = false;
            }
            else
            {
                cboProjectName1.SelectedIndex = 0;
            }
        }

        public void LoadPublisher()
        {
            cboPublisher.Items.Add("Tất cả");
            BookDAL bDAL = new BookDAL();
            lishPublisher = bDAL.GetAllPublisher();
            foreach (string s in lishPublisher)
            {
                cboPublisher.Items.Add(s);
            }
            cboPublisher.SelectedIndex = 0;
        }

        public void LoadIssuer()
        {
            cboIssuer.Items.Add("Tất cả");
            BookDAL bDAL = new BookDAL();
            listIssuer = bDAL.GetAllIssuer();
            foreach (string s in listIssuer)
            {
                cboIssuer.Items.Add(s);
            }
            cboIssuer.SelectedIndex = 0;
        }

        public void LoadAuthor()
        {
            cboAuthor.Items.Add("Tất cả");
            AuthorDAL aDAL = new AuthorDAL();
            listAuthor = aDAL.GetAllAuthor();
            if (listAuthor != null)
            {
                foreach (Author au in listAuthor)
                {
                    cboAuthor.Items.Add(au.Name);
                }
            }
            cboAuthor.SelectedIndex = 0;
        }

        public void LoadGenre()
        {
            cboGenre.Items.Add("Tất cả");
            BookDAL bDAL = new BookDAL();
            listGenre = bDAL.GetAllGenre();
            foreach (string s in listGenre)
            {
                cboGenre.Items.Add(s);
            }
            cboGenre.SelectedIndex = 0;
        }

        public void LoadNewdgvFilter()
        {
            BookDAL bDAL = new BookDAL();
            lbfv = bDAL.GetAllBookByFilter();
            ConvertAndLoad();
        }

        public void ConvertAndLoad()
        {
            if (lbfv != null)
            {
                dgvFilter.DataSource = lbfv;
                dgvFilter.Refresh();
            }
            else
            {
                dgvFilter.DataSource = new List<Book_FilterView>();
                dgvFilter.Refresh();
            }
        }

        public void LoaddgvFilterByProject()
        {
            BookDAL bDAL = new BookDAL();
            lbfv = bDAL.GetAllBookByFilter(lpr[cboProjectName1.SelectedIndex-1].ProjectID);
            ConvertAndLoad();
        }



        private void cboProjectName1_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblBookTitle.Text = "";
            lblBookLocation.Text = "";
            if (superTabControl1.SelectedTabIndex == 0)
            {
                if (cboProjectName1.SelectedIndex == 0)
                {
                    LoadNewdgvFilter();
                }
                else
                {
                    LoaddgvFilterByProject();
                }
            }
            else
            {
                LoaddgvFilterByAdvanceFilter();
            }
        }


        private void tabBasicFilter_Click(object sender, EventArgs e)
        {
            cboProjectName1.SelectedIndex = 0;
        }

        private void txtISBN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtLowPrice_Leave(object sender, EventArgs e)
        {
            LoaddgvFilterByAdvanceFilter();
        }

        private void txtHighPrice_Leave(object sender, EventArgs e)
        {
            LoaddgvFilterByAdvanceFilter();
        }

     




        private void dgvFilter_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                RightClickMenu.Enabled = false;
            }
        }

        private void dgvFilter_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                RightClickMenu.Enabled = true;
                dgvFilter.Rows[e.RowIndex].Selected = true;
                rowClicked = e.RowIndex;
            }
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            Int64 isbn = Convert.ToInt64(dgvFilter[0, dgvFilter.SelectedRows[0].Index].Value);


            if (chooseBookEvent != null)
            {
                chooseBookEvent(isbn);
                this.Close();
            }
            else
            {
                frmAddOrder.ISBN = Convert.ToInt64(dgvFilter[0, dgvFilter.SelectedRows[0].Index].Value);
                frmAddOrder.status = false;
                frmAddOrder frm = new frmAddOrder();
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }

        private void btnShowTableNo_Click(object sender, EventArgs e)
        {
           
            
        }

        private void frmBookFilter_Activated(object sender, EventArgs e)
        {
            if (type == 1)
            {
                cboProjectName1.Enabled = false;
            }
            else
            {
                cboProjectName1.Enabled = true;
            }
        }

        private void cboProjectName2_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (dgvFilter.SelectedRows.Count > 0 && dgvFilter.SelectedRows[0].Index >= 0)
            {
                btnOrder_Click(null, null);
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (dgvFilter.SelectedRows.Count > 0 && dgvFilter.SelectedRows[0].Index >= 0)
            {
                btnShowTableNo_Click(null, null);
            }
        }

        private void cboIssuer_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoaddgvFilterByAdvanceFilter();
        }

        private void btnSold_Click(object sender, EventArgs e)
        {
            Project project = new Project(0);
            if (cboProjectName1.SelectedIndex != 0)
            {
                project = lpr[cboProjectName1.SelectedIndex - 1];
            }
            Int64 isbn = 0;
            if (txtISBN.Text != "")
            {
                isbn = Convert.ToInt64(txtISBN.Text);
            }
            string publisher = "";
            if (cboPublisher.SelectedIndex != 0)
            {
                publisher = cboPublisher.Text;
            }
            string title = "";
            if (txtTitle.Text != "")
            {
                title = txtTitle.Text;
            }
            string author = "";
            if (cboAuthor.SelectedIndex != 0)
            {
                author = cboAuthor.Text;
            }
            string issuer = "";
            if (cboIssuer.SelectedIndex != 0)
            {
                issuer = cboIssuer.Text;
            }
            string genre = "";
            if (cboGenre.SelectedIndex != 0)
            {
                genre = cboGenre.Text;
            }
            Int32 lowPrice = 0;
            if (txtLowPrice.Text != "")
            {
                lowPrice = Convert.ToInt32(txtLowPrice.Text);
            }
            Int32 highPrice = 0;
            if (txtHighPrice.Text != "")
            {
                highPrice = Convert.ToInt32(txtHighPrice.Text);
            }
            BookDAL bDAL = new BookDAL();
            if (cboProjectName1.SelectedIndex == 0)
            {
                Sold = !Sold;
                lbfv = bDAL.GetAllBookByFilter(0, isbn, publisher, title, author, issuer, genre, lowPrice, highPrice, "Sold", Sold);
            }
            else
            {
                Sold = !Sold;
                lbfv = bDAL.GetAllBookByFilter(project.ProjectID, isbn, publisher, title, author, issuer, genre, lowPrice, highPrice, "Sold", Sold);
            }
            ConvertAndLoad();
        }

        private void dgvFilter_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                lblBookTitle.Text = dgvFilter[1, e.RowIndex].Value.ToString();
                if (cboProjectName1.SelectedIndex == 0)
                {
                    lblBookLocation.Text = "";
                }
                else
                {   
                    lblBookLocation.Text = (new Project_BookDAL()).GetProject_BookByID(lpr[cboProjectName1.SelectedIndex - 1].ProjectID,(new BookDAL()).GetBookByISBN(Convert.ToInt64(dgvFilter[0,e.RowIndex].Value)).BookID).TableNumber.ToString();
                }
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            LoaddgvFilterByAdvanceFilter();
        }

        private void btnUnFilter_Click(object sender, EventArgs e)
        {
            txtISBN.Clear();
            txtTitle.Clear();
            txtHighPrice.Clear();
            txtLowPrice.Clear();
            cboAuthor.SelectedIndex = 0;
            cboGenre.SelectedIndex = 0;
            cboIssuer.SelectedIndex = 0;
            cboPublisher.SelectedIndex = 0;
            LoaddgvFilterByAdvanceFilter();
        }

       

        


    }

}